/*
 * Copyright 2011 Niklas Kyster Rasmussen
 * 
 * This file is part of WotXP.
 *
 * WotXP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * WotXP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WotXP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package net.nikr.wot.core;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import net.nikr.text.TextTable;
import net.nikr.wot.calc.Calc;
import net.nikr.wot.cmd.data.Data;
import net.nikr.wot.cmd.Cmd;


public class CmdProgram {

	private Data data = new Data();
	private final static NumberFormat format = new DecimalFormat("#,##0");
	
	public CmdProgram() {
		int avg = Cmd.getNumberInput("Enter average xp per battle (enter to skip): ", true);
		int crew = 0;
		if (avg > 0){
			crew = Cmd.getNumberInput("Enter number of crew members: ");
			data.setAvg(avg);
			data.setCrew(crew);
		} 
		
		data.setPrimary(Cmd.getNumberInput("Primary: "));
		if (data.getPrimary() == 100){
			data.setSecondary1(Cmd.getNumberInput("Secondary 1: "));
			if (data.getSecondary1() == 100){
				data.setSecondary2(Cmd.getNumberInput("Secondary 2: "));
				if (data.getSecondary2() == 100){
					data.setSecondary3(Cmd.getNumberInput("Secondary 3: "));
				}
			}
		}
		System.out.println("");
		
		TextTable nextLevels = buildNextLevelTable(10);
		nextLevels.write();
		
		System.out.println("");
		
		TextTable xpNeeded = buildXpNeededTable();
		xpNeeded.write();
		
		System.out.println("");
	}
	
	private TextTable buildNextLevelTable(int levels){
		int total = 0;
		TextTable table = new TextTable();
		table.setDefaultFormat(Integer.class, format);
		table.setSpacing(3);
		
		table.add("To Level");
		table.add("Xp");
		table.add("Total Xp");
		for (int a = data.getTraningLvl(); a < 100 && a < data.getTraningLvl()+levels; a++){
			table.nextRow();
			table.add((a+1));
			table.add(Calc.xpToNextLevel(a, data.getTraning()), TextTable.Alignment.RIGHT);
			total += Calc.xpToNextLevel(a, data.getTraning());
			table.add(total, TextTable.Alignment.RIGHT);
			
		}
		return table;
	}
	
	private TextTable buildXpNeededTable(){
		TextTable table = new TextTable();
		table.setDefaultFormat(Integer.class, format);
		
		table.add("To 100%");
		table.add("Primary", TextTable.Alignment.RIGHT);
		table.add("Secondary 1", TextTable.Alignment.RIGHT);
		table.add("Secondary 2", TextTable.Alignment.RIGHT);
		table.add("Secondary 3", TextTable.Alignment.RIGHT);
		
		table.nextRow();
		table.add("XP");
		table.add(data.getPrimaryXpLeft(), TextTable.Alignment.RIGHT);
		table.add(data.getSecondary1XpLeft(), TextTable.Alignment.RIGHT);
		table.add(data.getSecondary2XpLeft(), TextTable.Alignment.RIGHT);
		table.add(data.getSecondary3XpLeft(), TextTable.Alignment.RIGHT);
		
		if (data.isBattles()){
			table.nextRow();
			table.add("Battles");
			table.add(data.getPrimaryBattlesLeft(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary1BattlesLeft(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary2BattlesLeft(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary3BattlesLeft(), TextTable.Alignment.RIGHT);
			
			table.nextRow();
			table.add("Battles (Rapid)");
			table.add(data.getPrimaryBattlesLeftRapid(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary1BattlesLeftRapid(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary2BattlesLeftRapid(), TextTable.Alignment.RIGHT);
			table.add(data.getSecondary3BattlesLeftRapid(), TextTable.Alignment.RIGHT);
		}
		return table;
	}
	
}
